import {
  Get,
  Req,
  Controller,
  Post,
  UseGuards,
  Body,
  Query,
} from '@nestjs/common';
import { orderService } from './order.service';
import { Roles } from '../../auth/roles.decorator';
import { RolesGuard } from '../../auth/roles.guard';
import { createOrderDto, getListDto } from './order.dto';
import { Request, Response } from 'express';

@Controller('order')
export class orderController {
  constructor(private readonly orderService: orderService) {}

  /**
   * 创建订单
   *
   * @param {createOrderDto} body
   * @returns {Promise<object>}
   * @memberof orderController
   */
  @Post('/createOrder')
  @Roles('admin', 'user')
  @UseGuards(RolesGuard)
  createOrder(
    @Req() req: Request,
    @Body() body: createOrderDto,
  ): Promise<object> {
    let userInfo = req['userInfo'];
    return this.orderService.createOrder(
      body.money,
      userInfo.userId,
      body.goodsId,
    );
  }

  /** 根据ID获取订单信息
   *
   *
   * @param {string} id
   * @returns {Promise<object>}
   * @memberof orderController
   */
  @Get('/getOrderInfoById')
  @Roles('admin', 'user')
  @UseGuards(RolesGuard)
  getOrderInfoById(@Query('id') id: string): Promise<object> {
    return this.orderService.getOrderInfoById(Number(id));
  }

  /**
   * 支付成功通知
   *
   * @param {*} body
   * @returns {Promise<string>}
   * @memberof orderController
   */
  @Get('/payNotify')
  payNotify(@Query() body): Promise<string> {
    return this.orderService.payNotify(body.xml);
  }

  /** 订单列表
   *
   *
   * @param {string} openid
   * @returns {Promise<object>}
   * @memberof orderController
   */
  @Get('/getList')
  @Roles('admin')
  @UseGuards(RolesGuard)
  getList(@Req() req: Request, @Query() query: getListDto): Promise<object> {
    let userInfo = req['userInfo'];
    return this.orderService.getList(
      userInfo,
      query.page,
      query.pageSize,
      query.orderClass,
    );
  }

  /** 用户订单列表
   *
   *
   * @param {string} openid
   * @returns {Promise<object>}
   * @memberof orderController
   */
  @Get('/orderListByUser')
  @Roles('admin', 'user')
  @UseGuards(RolesGuard)
  orderListByUser(
    @Query('page') page: string,
    @Query('pageSize') pageSize: string,
    @Query('openid') openid: string,
  ): Promise<object> {
    return this.orderService.orderListByUser(
      Number(page),
      Number(pageSize),
      openid,
    );
  }
}
